package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.FriendRelationshipVO;
import com.ruoyi.system.domain.vo.FriendRequestVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FriendRequest;
import com.ruoyi.system.service.IFriendRequestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 好友申请Controller
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/system/request")
public class FriendRequestController extends BaseController<IFriendRequestService,FriendRequest, FriendRequestVO> {
    @Autowired
    private IFriendRequestService friendRequestService;


    /**
     * 查询单个好友申请   用于回显
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/getOneFriendRequest")
    public AjaxResult getOneFriendRequest(@RequestBody FriendRequestVO infoVO) {
        FriendRequestVO result = friendRequestService.getOneFriendRequest(infoVO);
        return success(result);
    }


    /**
     * 查询好友申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/selRequestList")
    public AjaxResult selRequestList(@RequestBody FriendRequestVO infoVO) {
        List<FriendRequestVO> result = friendRequestService.selRequestList(infoVO);
        return success(result);
    }


    /**
     * 处理好友申请单
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/handleFriendRequest")
    public AjaxResult handleFriendRequest(@RequestBody FriendRequestVO infoVO) {
        boolean result = friendRequestService.handleFriendRequest(infoVO);
        if( result){
            return success("操作成功");
        }
        return error("系统错误");
    }


    /**
     * 添加好友
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/addFriend")
    public AjaxResult addFriend(@RequestBody FriendRequestVO infoVO) {
        boolean result = friendRequestService.addFriend(infoVO);
        if(result){
            return success("添加成功");
        }
        return error("系统错误");
    }


    /**
     * 查询好友申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:request:list')")
    @GetMapping("/list")
    public TableDataInfo list(FriendRequest friendRequest)
    {
        startPage();
        List<FriendRequest> list = friendRequestService.selectFriendRequestList(friendRequest);
        return getDataTable(list);
    }

    /**
     * 导出好友申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:request:export')")
    @Log(title = "好友申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FriendRequest friendRequest)
    {
        List<FriendRequest> list = friendRequestService.selectFriendRequestList(friendRequest);
        ExcelUtil<FriendRequest> util = new ExcelUtil<FriendRequest>(FriendRequest.class);
        util.exportExcel(response, list, "好友申请数据");
    }

    /**
     * 获取好友申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:request:query')")
    @GetMapping(value = "/{requestId}")
    public AjaxResult getInfo(@PathVariable("requestId") Long requestId)
    {
        return success(friendRequestService.selectFriendRequestByRequestId(requestId));
    }

    /**
     * 新增好友申请
     */
    @PreAuthorize("@ss.hasPermi('system:request:add')")
    @Log(title = "好友申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FriendRequest friendRequest)
    {
        return toAjax(friendRequestService.insertFriendRequest(friendRequest));
    }

    /**
     * 修改好友申请
     */
    @PreAuthorize("@ss.hasPermi('system:request:edit')")
    @Log(title = "好友申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FriendRequest friendRequest)
    {
        return toAjax(friendRequestService.updateFriendRequest(friendRequest));
    }

    /**
     * 删除好友申请
     */
    @PreAuthorize("@ss.hasPermi('system:request:remove')")
    @Log(title = "好友申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requestIds}")
    public AjaxResult remove(@PathVariable Long[] requestIds)
    {
        return toAjax(friendRequestService.deleteFriendRequestByRequestIds(requestIds));
    }
}
